package ensib.sti.ssu.activities;

import java.util.List;


import ensib.sti.ssu.R;
import ensib.sti.ssu.DAO.BlocNoteDAO;
import ensib.sti.ssu.beans.BlocNote;
import ensib.sti.ssu.classes.Parler;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.TextView;

public class RappelActivity extends Activity {
	Parler jeparle;
	  Thread t;
	  int n ;
	  int indice=0;
	  private int CODE_PREFERENCE_ACTIVITY=0x1128;
		private int CODE_PREFERENCE_SAUVEGARDE_ACTIVITY=0x1006;
		
		public final static int AJOUT_NOTE_ACTIVITY_CODE=0x1000;
		public final static int SAUVEGARDE_ACTIVITY_CODE=0x1002;
		public final static int RESTAURATION_DONNEES_ACTIVITY_CODE=0x1002;
		public final static int ACTIVITE_AJOUT_NOTE_CODE=0x1020;
	Button annuller;
	TextView nomtache,description;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rappel);
		BlocNoteDAO dao=new BlocNoteDAO(this);
		dao.open();
		final List<BlocNote> listerapelle = dao.listeTacheAexecuter();
		dao.close();
		if(listerapelle.size()<=0)
			finish();
		else{
			jeparle=new Parler(getApplicationContext());
			  n=listerapelle.size();
			 annuller=(Button) findViewById(R.id.rappel_button);
			 nomtache=(TextView) findViewById(R.id.rappel_nom_tache);
			 description=(TextView) findViewById(R.id.rappel_description_tache);
		 
    		 annuller.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					RappelActivity.this.finish();
				}
			});
		 t=new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true){
					if(indice>=n)indice=0;
				BlocNote note = listerapelle.get(indice++);
				jeparle.setMessage(getResources().getString(R.string.message_tache_a_executer)+":"+note.getNomTache()+", date ,"+note.getDeadLineDateToString()
						+" description,"+note.getDescription()).parle();
				//nomtache.setText(note.getNomTache());
				//description.setText(note.getDescription());
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					 
				}
				}
			}
		});
		t.start();
		 
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
            	if(!prefs.getBoolean("activer_rappel_tache_key", false))
    	  finish();
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		if(t!=null)t=null;
		if(jeparle!=null)jeparle.arreter();
		super.finish();
	}
	@SuppressLint("NewApi")
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.bloc_note, menu);
		// Associate searchable configuration with the SearchView
	    SearchManager searchManager =
	           (SearchManager) getSystemService(Context.SEARCH_SERVICE);
	    SearchView searchView =
	            (SearchView) menu.findItem(R.id.search).getActionView();
	    searchView.setSearchableInfo(
	            searchManager.getSearchableInfo(getComponentName()));
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

  final int id=item.getItemId();
  switch (id) {
case  R.id.preferencesetting:
	startActivityForResult(new Intent(this, Preference.class), CODE_PREFERENCE_ACTIVITY);
	break;
case R.id.parametresauvegarde:
	startActivityForResult(new Intent(this, PreferencesSauvegarde.class), CODE_PREFERENCE_SAUVEGARDE_ACTIVITY);
	break;
case R.id.abousUs:
	jeparle.setMessage(getResources().getString(R.string.aboutusmessage)).parle();
default:
	break;
}
  return true;
  
	}

}
